package exec

import "io"

func (c *Command) OnStateChanged(osc func(c *Command)) *Command {
	c.locker.Lock()
	defer c.locker.Unlock()
	if c.cur == nil {
		c.osc = osc
	}
	return c
}

func (c *Command) SetBackground(bg bool) *Command {
	c.locker.Lock()
	defer c.locker.Unlock()
	if c.cur == nil {
		c.bg = bg
	}
	return c
}

func (c *Command) Front() *Command {
	return c.SetBackground(false)
}

func (c *Command) Background() *Command {
	return c.SetBackground(true)
}

func (c *Command) ErrOut(out io.Writer) *Command {
	c.locker.Lock()
	defer c.locker.Unlock()
	if c.cur == nil {
		c.errOut = out
	}
	return c
}

func (c *Command) StdOut(out io.Writer) *Command {
	c.locker.Lock()
	defer c.locker.Unlock()
	if c.cur == nil {
		c.stdOut = out
	}
	return c
}

func (c *Command) NoErrOut() *Command {
	return c.ErrOut(DevNull)
}

func (c *Command) NoStdOut() *Command {
	return c.StdOut(DevNull)
}

func (c *Command) NoOut() *Command {
	return c.NoStdOut().NoErrOut()
}

func (c *Command) SetDir(dir string) *Command {
	if dir != "" {
		c.locker.Lock()
		defer c.locker.Unlock()
		if c.cur == nil {
			c.dir = dir
		}
	}
	return c
}
